home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / PRINT.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  51KB  |  1,510 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. #include "jwp.h"
  4. #include <math.h>
  5.  
  6.  
  7. MEASUREINFO Measurements[] = {
  8.     { "inches",         "\"",       2,  1.00 },
  9.     { "centimeters",    " cm",      1,  2.536 },
  10.     { "millimeters",    " mm",      0,  25.36 },
  11.     { "points",         " pts",     1,  72.00 },
  12.     { "pixels",         " dots",    0,  0.00 },
  13.     { NULL,             NULL,       0,  0.00 }
  14. };
  15.  
  16. static int PrintFrom, PrintTo;
  17. static HWND printdlghwnd;
  18. static BOOL UserAbort, PrintError;
  19.  
  20.  
  21. #define ESCAPECHAR      '&'
  22.  
  23. static struct {
  24.     char escape;
  25.     int summary;
  26. } HeaderStrings[] = {
  27.     { 'L', 0 },
  28.     { 'S', 1 },
  29.     { 'A', 2 },
  30.     { 'K', 3 },
  31.     { 'C', 4 },
  32.     { 'T', -1 },
  33.     { 'D', -2 },
  34.     { 'P', -3 },
  35.     { 'F', -4 },
  36.     { '\0', 0 }
  37. };
  38.  
  39.  
  40.  
  41. MEASUREMENT FindMeasurement (char *name, MEASUREMENT measure)
  42. {
  43.     int i;
  44.  
  45.     if (measure >= 0) {
  46.         strcpy(name, Measurements[measure].name);
  47.         return (measure);
  48.     }
  49.  
  50.     for (i = 0; Measurements[i].name != NULL; i++) {
  51.         if (!stricmp(name, Measurements[i].name)) return ((MEASUREMENT) i);
  52.     }
  53.     return (-1);
  54. }
  55.  
  56.  
  57.  
  58. HDC GetPrinterDC (BOOL IcOnly, char *name)
  59. {
  60.     char buffer[MAXLINELEN];
  61.     char *device, *driver, *output;
  62.  
  63.     /* Opens the printer device */
  64.  
  65.     GetProfileString ("windows", "device", ",,,", buffer, MAXLINELEN);
  66.  
  67.     if ((device = strtok(buffer, ",")) &&
  68.         (driver = strtok(NULL, ", ")) &&
  69.         (output = strtok(NULL, ", "))) {
  70.             if (name != NULL) sprintf(name, "%s on %s", device, output);
  71.  
  72.             if (IcOnly) {
  73.                 return (CreateIC(driver, device, output, NULL));
  74.             } else {
  75.                 return (CreateDC(driver, device, output, NULL));
  76.             }
  77.     }
  78.  
  79.     if (name != NULL) strcpy(name, "(None)");
  80.     return (NULL);
  81. }
  82.  
  83.  
  84.  
  85. BOOL SetupPrinter (BOOL ReloadDriver)
  86. {
  87.     HDC hdc;
  88.     POINT printdots;
  89.     double temp;
  90.     double nr_points[2], dpp[2];
  91.     char buffer[MAXLINELEN];
  92.  
  93.     if (ReloadDriver) {
  94.         hdc = GetPrinterDC(TRUE, buffer);
  95.         if (hdc == NULL) {
  96.             ErrorMessage(global.hwnd, "There are no printer currently connected.\n\n");
  97.             hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
  98.             global.printer = "DISPLAY";
  99.         } else if (!(GetDeviceCaps(hdc, RASTERCAPS) & RC_BITBLT)) {
  100.             ErrorMessage(global.hwnd, "The current printer is incapable of printing bitmaps.\n\n"
  101.                                          "You will not be able to print.");
  102.             DeleteDC(hdc);
  103.             hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
  104.             global.printer = "DISPLAY";
  105.         } else {
  106.             global.printer = DupString(buffer);
  107.         }
  108.  
  109.         global.resolution.x = GetDeviceCaps(hdc, LOGPIXELSX);
  110.         global.resolution.y = GetDeviceCaps(hdc, LOGPIXELSY);
  111.  
  112.         global.printable.x = GetDeviceCaps(hdc, HORZRES);
  113.         global.printable.y = GetDeviceCaps(hdc, VERTRES);
  114.  
  115.         Measurements[M_PIXELS].conversion = global.resolution.y;
  116.  
  117.         global.paper[0] = (double) GetDeviceCaps(hdc, HORZSIZE) / Measurements[M_MM].conversion;
  118.         global.paper[1] = (double) GetDeviceCaps(hdc, VERTSIZE) / Measurements[M_MM].conversion;
  119.     }
  120.  
  121.     dpp[0] = (double) global.resolution.x / 72.0;
  122.     dpp[1] = (double) global.resolution.y / 72.0;
  123.  
  124.  
  125.     /* We compensate for the printer's aspect ratio.  If necessary, */
  126.     /* stretching or compressing the horizontal axis.               */
  127.  
  128.     printdots.y = PRINTFONT->height * global.printscale;
  129.     temp = (double) PRINTFONT->width * (double) global.printscale;
  130.     temp = temp * (double) global.resolution.x / (double) global.resolution.y;
  131.     printdots.x = temp;
  132.  
  133.     /* This is the number of points per printing font */
  134.  
  135.     nr_points[0] = (double) printdots.x / dpp[0];
  136.     nr_points[1] = (double) printdots.y / dpp[1];
  137.  
  138.     global.dispscale = (double) BASEFONT->height / (double) PRINTFONT->height;
  139.  
  140.     if (ReloadDriver) DeleteDC(hdc);
  141.     return (TRUE);
  142. }
  143.  
  144.  
  145.  
  146. void InitPrinting (void)
  147. {
  148.     SetupPrinter(TRUE);
  149. }
  150.  
  151.  
  152. void ChangePrinterOrFonts (void)
  153. {
  154.     FILEOPTIONS *f;
  155.     HCURSOR hcursor;
  156.     HFONT hfont;
  157.     HDC hdc;
  158.     HWND FocusHwnd;
  159.  
  160.     hcursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  161.     ShowCursor(TRUE);
  162.  
  163.     FocusHwnd = SetFocus(NULL);
  164.  
  165.     SetupPrinter(TRUE);
  166.  
  167.     hdc = GetPrinterDC(TRUE, NULL);
  168.     if (hdc == NULL) hdc = CreateIC("DISPLAY", NULL, NULL, NULL);
  169.  
  170.     hfont = SelectAsciiFont(hdc, DefAsciiFont.facename, DefAsciiFont.size, NULL);
  171.  
  172.     if (DefAsciiFont.hfont != NULL) DeleteObject(DefAsciiFont.hfont);
  173.  
  174.     DefAsciiFont.hfont =
  175.         FindMatchingScreenFont(hfont, global.dispscale / global.printscale,
  176.                                &(DefAsciiFont.textmetric));
  177.     DeleteObject(hfont);
  178.     DeleteDC(hdc);
  179.  
  180.     FontCharWidth (0, -1);      /* Reload width tables */
  181.  
  182.     global.leading = (int) ((double) PRINTFONT->leading * global.dispscale);
  183.     if (global.leading % 2 != 0) global.leading++;
  184.  
  185.     global.spacing = (int) ((double) PRINTFONT->spacing * global.dispscale);
  186.     if (global.spacing % 2 != 0) global.spacing++;
  187.  
  188.     for (f = fileoptions; f != NULL; f = f->next) {
  189.         f->leading = global.leading;
  190.         f->spacing = global.spacing;
  191.         f->basefont = BASEFONT;
  192.         f->linelen = CalcLineLength(f);
  193.         ReformatFile(f);
  194.  
  195.         if (!FindCaret(f, TRUE)) MoveIntoWindow(f);
  196.         InvalidateRect(f->hwnd, NULL, TRUE);
  197.         if(RULER(f).hwnd != NULL) InvalidateRect(RULER(f).hwnd, NULL, TRUE);
  198.     }
  199.  
  200.     ShowCursor(FALSE);
  201.     SetCursor(hcursor);
  202.  
  203.     if (FocusHwnd != NULL) SetFocus(FocusHwnd);
  204. }
  205.  
  206.  
  207.  
  208. static void FormatTimeDate (char *timep, char *datep)
  209. {
  210.     time_t t;
  211.     struct tm *tmp;
  212.     static int idate, itime;
  213.     static char sdate[3], stime[3], sam[6], spm[6];
  214.     static BOOL Filled = FALSE;
  215.  
  216.     if (!Filled || (timep == NULL && datep == NULL)) {
  217.         idate = GetProfileInt("intl", "iDate", 0);
  218.         itime = GetProfileInt("intl", "iTime", 0);
  219.  
  220.         GetProfileString("intl", "sDate", "/", sdate, 2);
  221.         GetProfileString("intl", "sTime", ":", stime, 2);
  222.         GetProfileString("intl", "s1159", "AM", sam, 5);
  223.         GetProfileString("intl", "s2359", "PM", spm, 5);
  224.         Filled = TRUE;
  225.     }
  226.  
  227.     time(&t);
  228.     tmp = localtime(&t);
  229.  
  230.     if (datep != NULL) {
  231.         sprintf(datep, "%d%s%02d%s%02d",
  232.                 idate == 1 ? tmp->tm_mday : idate == 2 ? (tmp->tm_year % 100) : (tmp->tm_mon + 1), sdate,
  233.                 idate == 1 ? (tmp->tm_mon + 1) : idate == 2 ? (tmp->tm_mon + 1) : tmp->tm_mday, sdate,
  234.                 idate == 1 ? (tmp->tm_year % 100) : idate == 2 ? tmp->tm_mday : (tmp->tm_year % 100));
  235.     }
  236.     if (timep != NULL) {
  237.         if (itime == 1) {
  238.             sprintf(timep, "%02d%s%02d", tmp->tm_hour, stime, tmp->tm_min);
  239.         } else {
  240.             sprintf(timep, "%d%s%02d %s",
  241.                     (tmp->tm_hour % 12) ? (tmp->tm_hour % 12) : 12, stime,
  242.                     tmp->tm_min, (tmp->tm_hour / 12) ? spm : sam);
  243.         }
  244.     }
  245. }
  246.  
  247.  
  248.  
  249. static void BuildHeaderString (FILEOPTIONS *f, KANJI far *kp, int PageNum, KANJI far *out)
  250. {
  251.     int i, j, k;
  252.     char cch;
  253.     KANJI kch;
  254.     KANJI far *kp1;
  255.  
  256.     j = 0;
  257.  
  258.     for (i = 0; kp[i]; i++) {
  259.         kch = kp[i];
  260.         if (ISKANJI(kch)) cch = LOBYTE(TranslateJAscii(kch, TRUE));
  261.         else cch = LOBYTE(kch);
  262.         if ('a' <= cch && cch <= 'z') cch -= 32;
  263.  
  264.         if (cch == ESCAPECHAR) {
  265.             i++;
  266.             if (ISKANJI(kp[i])) cch = LOBYTE(TranslateJAscii(kp[i], TRUE));
  267.             else cch = LOBYTE(kp[i]);
  268.             if ('a' <= cch && cch <= 'z') cch -= 32;
  269.  
  270.             for (k = 0; HeaderStrings[k].escape; k++) {
  271.                 if (HeaderStrings[k].escape == cch) break;
  272.             }
  273.  
  274.             if (!HeaderStrings[k].escape) {
  275.                 out[j++] = kch; out[j++] = kp[i]; break;
  276.             } else if (HeaderStrings[k].summary >= 0) {
  277.                 kp1 = f->summary[HeaderStrings[k].summary];
  278.                 if (kp1 != NULL) for (k = 0; kp1[k]; k++) out[j++] = kp1[k];
  279.             } else {
  280.                 char buffer[50];
  281.  
  282.                 switch (HeaderStrings[k].summary) {
  283.                     case -1:    /* Time */